package com.balloon.springboot.generate.generate.web;

import com.balloon.springboot.core.rules.ResultVO;
import com.balloon.springboot.core.rules.ResultVOUtils;
import com.balloon.springboot.core.rules.SelectFormatVO;
import com.balloon.springboot.generate.common.properties.GenerateProperties;
import com.balloon.springboot.generate.generate.dto.GenerateCodeDto;
import com.balloon.springboot.generate.generate.service.GenerateCodeService;
import com.balloon.springboot.utils.FieldInfo;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.List;


/**
 * 代码生成器控制器
 *
 * @author liaofuxing
 * @E-mail liaofuxing@outlook.com
 * @date 2021/09/11 19:32
 **/
@Controller
@RequestMapping("/generateCode")
public class GenerateController {

    @Autowired
    private GenerateProperties generateProperties;
    @Autowired
    private GenerateCodeService generateCodeService;

    /**
     * 获取表名下拉框
     *
     * @return ResultVO<List < SelectFormatVO>>
     * @throws SQLException SQL执行异常
     */
    @GetMapping("/getMysqlTableSelect")
    @ResponseBody
    public ResultVO<List<SelectFormatVO>> getTableSelect() throws SQLException {
        List<SelectFormatVO> mysqlTableSelect = generateCodeService.getTableSelect();
        return ResultVOUtils.success(mysqlTableSelect);
    }

    /**
     * 获取表字段列表
     *
     * @param tableName 表名
     * @return ResultVO<List < SelectFormatVO>>
     * @throws SQLException SQL执行异常
     */
    @GetMapping("/getTableField")
    @ResponseBody
    public ResultVO<List<FieldInfo>> getTableField(String tableName) throws SQLException {
        List<FieldInfo> tableField = generateCodeService.getTableField(tableName);
        return ResultVOUtils.success(tableField);
    }

    /**
     * 生成代码
     *
     * @param generateCodeDto 代码生成器Dto
     * @param request         request
     * @param response        response
     * @throws SQLException      SQL执行异常
     * @throws IOException       io异常
     * @throws TemplateException 模板异常
     */
    @PostMapping("/generateCode")
    @ResponseBody
    public void generateCode(@RequestBody GenerateCodeDto generateCodeDto, HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, TemplateException {
        String token = request.getHeader("token");
        generateCodeService.generateCode(generateCodeDto, token);
        String filePath = generateProperties.getGenerateFileRootPath() + "/" + token + "zip" + "/" + generateCodeDto.getTableName() + ".zip";
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        if (file.exists()) {
            // 设置强制下载不打开  
            response.setHeader("Content-Disposition", "attachment; filename=" + generateCodeDto.getTableName() + ".zip");
            byte[] buffer = new byte[1024];

            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while (i != -1) {
                    response.getOutputStream().write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                response.flushBuffer();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                assert fis != null;
                fis.close();
                assert bis != null;
                bis.close();
            }
        }
    }

}
